package com.jackrain.nea.ad.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.jackrain.nea.ad.api.CmenuReloadCmd;
import com.jackrain.nea.ad.entity.MenuEntity;
import com.jackrain.nea.ad.mapper.CmenuMapper;
import com.jackrain.nea.ad.utility.MenuManager;
import com.jackrain.nea.config.Resources;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.ext.permission.Permissions;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import com.jackrain.nea.web.query.QuerySession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.awt.*;
import java.util.HashMap;
import java.util.List;

/**
 * @author lzm
 * @date 2017-12-2
 */
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "ad")
public class CmenuReloadCmdImpl extends CommandAdapter implements CmenuReloadCmd {
    @Override
    public ValueHolder execute(QuerySession session) throws NDSException {
        CmenuMapper dal = ApplicationContextHandle.getBean(CmenuMapper.class);

        List<MenuEntity> allMenus = dal.fetchAllMenus();
        List<HashMap<String, Long>> allMenuActions = dal.fetchAllActions();
        log.debug("menu.reload.start!");
        MenuManager.updateAll(session, allMenus, allMenuActions);
        Permissions.getMenuTree(true);
        Permissions.getMenuTreeInDevMode(true);
        ValueHolder result = new ValueHolder();
        result.put("code", 0);
        result.put("message", Resources.getMessage("Success", session.getLocale()));
        return result;
    }
}
